﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MoveToLocation.CApMoveToLocationEnable
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.MoveToLocation;

internal class CApMoveToLocationEnable : ITxCommandEnabler
{
  private bool m_isCommandStarted;

  private bool IsSimulationRunning
  {
    get
    {
      return TxApplication.ActiveDocument.PlatformGlobalServicesProvider.IsDataLoaded() && TxApplication.ActiveDocument.SimulationPlayer.IsSimulationRunning();
    }
  }

  private bool CheckEnable()
  {
    bool flag = false;
    if (TxApplication.ActiveDocument.PlatformGlobalServicesProvider.IsDataLoaded())
    {
      if (!this.IsSimulationRunning)
      {
        TxSelection activeSelection = TxApplication.ActiveSelection;
        TxTypeFilter txTypeFilter = new TxTypeFilter();
        txTypeFilter.AddIncludedType(typeof (ITxRoboticLocationOperation));
        txTypeFilter.AddIncludedType(typeof (ITxRoboticOrderedCompoundOperation));
        txTypeFilter.AddIncludedType(typeof (TxRoboticSeamOperation));
        TxObjectList filteredItems = activeSelection.GetFilteredItems((ITxTypeFilter) txTypeFilter);
        if (((Collection<ITxObject>) filteredItems).Count == 1)
        {
          try
          {
            ITxRoboticOrderedCompoundOperation compoundOperation = (ITxRoboticOrderedCompoundOperation) null;
            ITxObject itxObject = ((Collection<ITxObject>) filteredItems)[0];
            switch (itxObject)
            {
              case ITxRoboticLocationOperation _:
                ITxObject collection = (ITxObject) itxObject.Collection;
                if (collection is TxRoboticSeamOperation)
                  collection = (ITxObject) collection.Collection;
                compoundOperation = collection as ITxRoboticOrderedCompoundOperation;
                break;
              case TxRoboticSeamOperation _:
                compoundOperation = (ITxObject) itxObject.Collection as ITxRoboticOrderedCompoundOperation;
                break;
              case ITxRoboticOrderedCompoundOperation _:
                compoundOperation = itxObject as ITxRoboticOrderedCompoundOperation;
                break;
            }
            if (compoundOperation != null && ((ITxObjectCollection) compoundOperation).Count == 0)
              compoundOperation = (ITxRoboticOrderedCompoundOperation) null;
            if (compoundOperation != null)
              flag = TxRobot.op_Inequality(compoundOperation.Robot as TxRobot, (ITxObject) null);
          }
          catch (Exception ex)
          {
            flag = false;
          }
        }
      }
      else if (this.m_isCommandStarted)
        flag = true;
    }
    return flag;
  }

  public bool Enable
  {
    get
    {
      if (!this.CheckEnable())
        return false;
      return !this.IsSimulationRunning || this.m_isCommandStarted;
    }
  }

  public bool IsCommandStrated
  {
    set => this.m_isCommandStarted = value;
  }
}
